<template>
    <div class="p-2">
        <el-card shadow="never">
            <template #header>
                <el-form :model="queryParams" ref="queryFormRef" :inline="true">
                    <el-form-item label="申请日期：" prop="timeDate">
                        <el-date-picker v-model="queryParams.timeDate" type="daterange" range-separator="至"
                            start-placeholder="开始时间" end-placeholder="结束时间" @change="handleTimeChange"
                            format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
                    </el-form-item>
                    <el-form-item label="供应商：" prop="orderPrintStatus">
                        <el-select v-model="queryParams.orderPrintStatus" clearable>
                            <el-option label="未打印" value="0"></el-option>
                            <el-option label="已打印" value="1"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="入库状态：" prop="orderPrintStatus">
                        <el-select v-model="queryParams.orderPrintStatus" clearable>
                            <el-option label="未打印" value="0"></el-option>
                            <el-option label="已打印" value="1"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="付款状态：" prop="orderPrintStatus">
                        <el-select v-model="queryParams.orderPrintStatus" clearable>
                            <el-option label="未打印" value="0"></el-option>
                            <el-option label="已打印" value="1"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="显示顺序：" prop="orderPrintStatus">
                        <el-select v-model="queryParams.orderPrintStatus" clearable>
                            <el-option label="未打印" value="0"></el-option>
                            <el-option label="已打印" value="1"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="模糊查询：" prop="workName">
                        <el-input v-model="queryParams.workName" placeholder="请输入"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>
            </template>
            <el-table v-loading="loading" :data="dataList" stripe>
                <el-table-column label="所属公司" prop="companyName" align="center"></el-table-column>
                <el-table-column label="申请人" prop="applyName" align="center"></el-table-column>
                <el-table-column label="申请日期" prop="applyDate" align="center" width="120"></el-table-column>
                <el-table-column label="交货日期" align="center" width="120">
                    <template #default="scope">
                        <span :style="deliveryDateStyle(scope.row.deliveryDate)"> {{ scope.row.deliveryDate }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="采购单号" prop="purchaseOrderCode" align="center"></el-table-column>
                <el-table-column label="材料小类" prop="productSubcategoryName" align="center"></el-table-column>
                <el-table-column label="品名及规格" prop="specsDetail" align="center"></el-table-column>
                <el-table-column label="效果图" align="center">
                    <template #default="scope">
                        <div class="demo-image__preview">
                            <el-image style="width: 100px; height: 100px" lazy :src="scope.row.imageUrl"
                                preview-teleported :preview-src-list="[scope.row.imageUrl]" fit="cover" />
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="申请数量" prop="number" align="center"></el-table-column>
                <el-table-column label="采购数量" prop="purchaseNumber" align="center"></el-table-column>
                <el-table-column label="单位" prop="purchaseUnit" align="center">
                    <template #default="scope">
                        {{ getUnitLabel(scope.row.purchaseUnit) }}
                    </template>
                </el-table-column>
                <el-table-column label="入库数量" prop="inNumber" align="center"></el-table-column>
                <el-table-column label="供应商" prop="supplyFactoryName" align="center"></el-table-column>
                <el-table-column label="备注" prop="remark" align="center">
                    <template #default="scope">
                        <el-button @click="handleViewNotes(scope.row)" type="primary" link>查看备注</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="入库日期" prop="recentInDate" align="center" width="120"></el-table-column>
                <el-table-column label="入库状态" prop="customizedName" align="center">
                    <template #default="scope">
                        <span v-if="scope.row.inStatus == '0'" style="color: #F75F51;">未入库</span>
                        <span v-else-if="scope.row.inStatus == '1'" style="color: #E6A33F;">部分入库</span>
                        <span v-else-if="scope.row.inStatus == '2'" style="color: #68C23D;">已入库</span>
                    </template>
                </el-table-column>
                <el-table-column label="对账状态" prop="customizedName" align="center">
                    <template #default="scope">
                        <span v-if="scope.row.reconciliationFlag == '0'" style="color: #F75F51;">未对账</span>
                        <span v-else-if="scope.row.reconciliationFlag == '1'" style="color: #E09FF9;">已对账</span>
                        <el-button v-else-if="scope.row.reconciliationFlag == '2'" type="success" link>已付款</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="验收报告" width="120px" prop="customizedName" align="center">
                    <template #default="scope">
                        <el-button v-if="scope.row.checkStatus == '1'" @click="handleOpenPurchaseCollect(scope.row)"
                            type="primary" link>查看验收信息</el-button>
                        <el-button v-else type="danger" @click="handleOpenPurchaseCollect(scope.row)"
                            link>没有验收信息</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" align="center" width="240px">
                    <template #default="scope">
                        <el-tooltip content="采购入库">
                            <el-button @click="handleOpenPurchase(scope.row)" link :style="{ fontSize: '13px' }"
                                type="primary" icon="CopyDocument"></el-button>
                        </el-tooltip>
                        <el-tooltip content="申请对账" placement="top">
                            <el-button @click="handleOpenApply(scope.row)" link :style="{ fontSize: '13px' }"
                                type="warning" icon="Money"></el-button>
                        </el-tooltip>
                        <el-tooltip content="撤销申请对账">
                            <el-button @click="revokeOpenApply(scope.row, getList)" link :style="{ fontSize: '13px' }"
                                type="danger" icon="RefreshLeft"></el-button>
                        </el-tooltip>
                        <el-tooltip content="详情" placement="top">
                            <el-button @click="handleOpenDetail(scope.row)" link :style="{ fontSize: '13px' }"
                                type="success" icon="Document"></el-button>
                        </el-tooltip>
                        <el-tooltip content="删除">
                            <el-button @click="handleDelete(scope.row, getList)" link :style="{ fontSize: '13px' }"
                                type="danger" icon="CircleClose"></el-button>
                        </el-tooltip>
                        <el-tooltip content="发货标识" placement="top">
                            <el-button @click="handleDeliverGoods(scope.row, getList)" link
                                :style="{ fontSize: '13px' }" type="success" icon="Van"></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>

            </el-table>
            <pagination v-show="totalData > 0" :total="totalData" v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize" @pagination="getList" />
        </el-card>

        <!-- 采购入库弹窗 -->
        <PurchaseReceipt :visible="purchaseVisible" :initList="purchaseDetails" :purchaseRow="purchaseRow"
            @close="purchaseVisible = false" @submit="getList" />
        <!-- 查看备注弹窗 -->
        <ViewNotes :visible="viewNotesVisible" :initList="viewNotesObj" @close="viewNotesVisible = false" />
        <!-- 查看已付款信息 --> 
        <!-- <PaymentInformation :visible="paymentVisible" @close="paymentVisible = false" /> -->
        <!-- 申请对账 -->
        <ApplyForReconciliation :visible="applyVisible" :initList="applyObj" @submit="getList"
            @close="applyVisible = false" />
        <!-- 查看详情 -->
        <OrderDetails :visible="detailVisible" :initList="detailRow" :dataList="detailObj"
            @close="detailVisible = false" />
        <!-- 采购验收 -->
        <Purchase @submit="getList" :visible="purchaseCollectVisible" :initList="purchaseCollectRow"
            :dataList="purchaseCollectDetails" @close="purchaseCollectVisible = false" />
    </div>
</template>
<script setup lang="ts">
import { type FormInstance } from 'element-plus';
import { getPendingStorageListApi } from '@/api/sj/purchaseManagement/toBeStored';

/** 查看详情 */
import OrderDetails from './components/details.vue';

/** 采购入库 */
import PurchaseReceipt from './components/purchaseReceipt.vue';
import usePurchaseReceipt from '@/hooks/purchaseManagement/toBeStored/usePurchaseReceipt';

/** 查看备注 */
import ViewNotes from './components/viewNotes.vue';
import useViewNotes from '@/hooks/purchaseManagement/toBeStored/useViewNotes';

/** 查看付款信息 付款凭证 */
// import PaymentInformation from './components/paymentInformation.vue';
// import usePaymentInformation from '@/hooks/purchaseManagement/toBeStored/usePaymentInformation';
// const { paymentVisible } = usePaymentInformation();

/** 申请对账 */
import ApplyForReconciliation from './components/applyForReconciliation.vue';
import useApplyForReconciliation from '@/hooks/purchaseManagement/toBeStored/useApplyForReconciliation';

/** 采购验收 */
import Purchase from './components/purchase.vue';
import usePurchase from '@/hooks/purchaseManagement/toBeStored/usePurchase';
const { purchaseCollectVisible, purchaseCollectRow, purchaseCollectDetails, handleOpenPurchaseCollect } = usePurchase();
const { purchaseVisible, purchaseDetails, purchaseRow, handleOpenPurchase, handleDelete, handleDeliverGoods } = usePurchaseReceipt();
const { viewNotesVisible, detailVisible, viewNotesObj, detailObj, detailRow, handleViewNotes, handleOpenDetail } = useViewNotes();
const { applyVisible, applyObj, handleOpenApply, revokeOpenApply } = useApplyForReconciliation();

const { proxy } = getCurrentInstance() as any;
const { unit_enumeration } = toRefs(proxy?.useDict('unit_enumeration'));

const queryFormRef = ref<FormInstance>();

const data = reactive<pageDataOnlyQuery<any>>({
    queryParams: {
        timeDate: '',
        startDate: '',
        endDate: '',
        pageSize: 10,
        pageNum: 1
    }
});

const { queryParams } = toRefs(data);

const loading = ref(false);
const dataList = ref<any>([]);
const totalData = ref(0);

// 辅助函数获取单位标签
const getUnitLabel = (unitValue: string): string => {
    const unit = unit_enumeration.value?.find((item: any) => item.value === unitValue);
    return unit?.label || '';
};

const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
};

const resetQuery = () => {
    queryFormRef.value?.resetFields();
    queryParams.value.startDate = '';
    queryParams.value.endDate = '';
    queryParams.value.timeDate = '';
    handleQuery();
};

/** 日期选择 */
const handleTimeChange = (val: any) => {
    queryParams.value.startDate = val[0];
    queryParams.value.endDate = val[1];
};

const getList = async () => {
    try {
        const { data, code } = await getPendingStorageListApi(queryParams.value);

        if (code == 200) {
            dataList.value = data.records;
            totalData.value = data.total;
        }

    } catch (error) {

    }
};

const deliveryDateStyle = (deliveryDate: string) => {
    const currentDate = new Date().getTime(); // 获取当前日期的时间戳
    const deliveryDateObj = new Date(deliveryDate).getTime(); // 获取交货日期的时间戳
    const timeDifference = currentDate - deliveryDateObj; // 计算时间差
    // 判断交货日期是否在当前日期前后一天内
    if (timeDifference > 0) {
        return { color: 'red' };
    }
};

onMounted(() => {
    getList();
});

</script>
<style lang="scss" scoped>
.red {
    color: red;
}

.orange {
    color: orange;
}

.green {
    color: green;
}

.purple {
    color: #DF9BF9;
}
</style>